package com.cqvip.innocence.project.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cqvip.innocence.project.model.dto.router.Meta;
import com.cqvip.innocence.project.model.dto.router.Routers;
import com.cqvip.innocence.project.model.entity.Resource;
import com.cqvip.innocence.project.mapper.ResourceMapper;
import com.cqvip.innocence.project.model.entity.Role;
import com.cqvip.innocence.project.model.enums.MenuType;
import com.cqvip.innocence.project.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 *
 * </p>
 *
 * @author Innocence
 * @since 2020-07-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

    @Override
    public List<Resource> getResourcesByRoleId(Role role) {
        return baseMapper.getResourcesByRoleId(role.getId());
    }

    @Override
    public List<Routers> processRouter(List<Resource> resources) {
        List<Routers> routers = new ArrayList<>();
        Iterator<Resource> iterator = resources.iterator();
        while (iterator.hasNext()){
            Routers out = new Routers();
            Resource next = iterator.next();
            //组装固定meta和其他固定信息
            Meta meta = new Meta();
            meta.setIcon(next.getMenuIcon());
            meta.setNoCache(false);
            meta.setTitle(next.getName());
            out.setMeta(meta);
            out.setHidden(false);
            out.setRedirect("noRedirect");
            out.setId(next.getId());
            out.setParentId(next.getParentId());
            out.setAlwaysShow(false);
            String s = next.getMenuUrl().replace("/", "").toLowerCase();
            String newStr = s.substring(0, 1).toUpperCase() + s.substring(1);
            out.setName(newStr);
            //先组装一个外层router目录
            if (MenuType.M.equals(next.getMenuType())){
                out.setComponent("Layout");
                if (next.getParentId()==null){
                    out.setPath("/"+next.getMenuUrl().replace("/",""));
                }else {
                    out.setPath(next.getMenuUrl());
                }
            }else if (MenuType.C.equals(next.getMenuType())){
                if (next.getParentId()==null){
                    out.setComponent("Layout");
                    out.setPath("/");
                }else {
                    out.setPath(next.getMenuUrl());
                    if (StrUtil.isBlank(next.getComponent())){
                        out.setComponent(null);
                    }else {
                        out.setComponent(next.getComponent());
                    }
                }
                Routers inner = new Routers();
                inner.setName(newStr);
                inner.setPath("/"+next.getMenuUrl().replace("/",""));
                inner.setHidden(false);
                if (StrUtil.isBlank(next.getComponent())){
                    inner.setComponent(null);
                }else {
                    inner.setComponent(next.getComponent());
                }
                inner.setMeta(meta);
                List<Routers> list = new ArrayList<>();
                list.add(inner);
                out.setChildren(list);
            }
            routers.add(out);
        }
        return routers;
    }
}
